<?php
$form = $this->beginWidget('PsActiveForm', array(
	'id' => 'update-invoice-form',
	'enableAjaxValidation' => true,
	'enableClientValidation' => true,
	'clientOptions' => array(
		'validationDelay' => 200,
		'inputContainer' => 'tr',
		'validateOnSubmit' => true,
		'validateOnChange' => false,
	)
		));
?>
<script type="text/javascript">
	var currency_code = "<?php echo (isset($invoice->currency_code) ? $invoice->currency_code : '$'); ?>";
	var propCaseIdForNewInvoice = <?php echo ($propertyCaseId ? $propertyCaseId : 'null'); ?>;
	var propCaseFlag = 0;
	var billToName = '<?php echo ($propertyCaseBillToName ? $propertyCaseBillToName : 'null'); ?>';
	var gstRate = <?php echo Yii::app()->systemConfig->get('GstRate') ? Yii::app()->systemConfig->get('GstRate') : '0.00'; ?>;
	var propertyCaseBillto = <?php echo empty($propertyCaseBillto) ? "null" : '"' . $propertyCaseBillto . '"'; ?>;
</script>
<div id="invoice-info">
	<div class="grid_8">
		<div class="section">
			<div class="block">

				<div class="table-block">
					<table class="form-table">
						<tr style="display: table-row;">
							<th colspan="3" class="" style="text-align:left;">
						<h2>Billing Information</h2>
						</th>
						</tr>
						<tr id="searchOrAdd">
							<th colspan="2">
								<button id="btnChooseParty" class="link-button with-icon" type="button">
									<img src="/images/icons/fugue/magnifier.png" />
									Search Contacts or Agents
								</button>
								<button id="btAddContact" class="link-button with-icon" type="button">
									<img src="/images/icons/fugue/plus.png" /> 
									Add Contact 
								</button>
							</th>
						</tr>
						<tr id="billToDetails" class="bill-to-info" style="<?php echo ($invoice->isNewRecord ? "display: none" : ""); ?>">
							<th scope="row" width="140"><?php echo $form->labelEx($invoice, 'bill_to_name', array()); ?></th>
							<td>
								<?php echo $form->textField($invoice, 'bill_to_name'); ?>
								<?php echo $form->hiddenField($invoice, 'to_party_id'); ?>
								<div id="invoice_add_careof_attn_link">
									<a id="invoice-care-of" href="javascript:void(0)" class="blue link" style="margin-right: 5px; <?php echo (empty($invoice->care_of)) ? 'display:""' : 'display:none;'; ?>">Add C/O</a>
									<a id="invoice-attn" href="javascript:void(0)" class="blue link" style="margin-right: 5px; <?php echo (empty($invoice->attn)) ? 'display:""' : 'display:none;'; ?>">Add Attn</a>
								</div>
								<input type="hidden" id="model" name="model" value="<?php echo $model; ?>">
								<input type="hidden" id="propertyCaseId" name="model" value="<?php echo $propertyCaseId; ?>">
								<input type="hidden" id="propertyCaseBillToPartyId" name="model" value="<?php echo $propertyCaseBillToPartyId; ?>">
								<input type="hidden" id="newInvoice" name="model" value="<?php echo ($invoice->isNewRecord ? 1 : 0); ?>">

								<?php echo $form->hiddenField($invoice, 'contact_id'); ?>
								<?php echo $form->hiddenField($invoice, 'bill_to_id'); ?>
								<?php echo $form->error($invoice, 'bill_to_name', false); ?>
							</td>
						</tr>
						<tr id="editCareOf" style="<?php echo (empty($invoice->care_of)) ? 'display:none;' : 'display:""'; ?>">
							<th scope="row" width="140"><?php echo $form->labelEx($invoice, 'care_of', array('label' => "C/O Details")); ?></th>
							<td>
								<?php echo $form->textField($invoice, 'care_of'); ?>
							</td>
						</tr>
						<tr id="editAttn" style="<?php echo (empty($invoice->attn)) ? 'display:none;' : 'display:""'; ?>">
							<th scope="row" width="140"><?php echo $form->labelEx($invoice, 'attn', array('label' => "Attn Details")); ?></th>
							<td>
								<?php echo $form->textField($invoice, 'attn'); ?>
							</td>
						</tr>

						<tr id="editContactRow" class="bill-to-info" style="<?php echo ($invoice->isNewRecord ? "display: none" : ""); ?>">
							<th scope="row" width="140"><?php echo $form->labelEx($invoice, 'bill_to_address', array('label' => 'Address')); ?></th>
							<td>
								<div id="billToAddress"><?php echo nl2br($invoice->bill_to_address); ?></div>
								<a id="btnEditContact" href="javascript:void(0)" class="blue link">
									(Edit Address)
								</a>
								<?php echo $form->textArea($invoice, 'bill_to_address', array('rows' => 3)); ?>
								<?php echo $form->error($invoice, 'bill_to_address', false); ?>
							</td>
						</tr>

						<tr id="clearAll" style="display:none;">
							<th colspan="2">
								<button id="btnClearParty" class="link-button with-icon" type="button">
									<img src="/images/icons/fugue/arrow-return-180-left.png" />
									Clear
								</button>
							</th>
						</tr>
					</table>
				</div>
			</div>
		</div>
	</div>
	<div class="grid_8">
		<div class="section">
			<div class="block">
				<h2>Invoice Information</h2>
				<div class="table-block">
					<table class="form-table">
						<tr>
							<th scope="row" width="120"><?php echo $form->labelEx($invoice, 'invoice_number', array('label' => 'Invoice #')); ?></th>
							<td>
								<?php echo $form->textField($invoice, 'invoice_number', array('readonly' => ($this->action->id != 'create') ? 'readonly' : '')); ?>
								<?php if ($this->action->id == 'create'): ?>
									<?php echo $form->checkBox($invoice, 'automatic_numbering', array('uncheckValue' => 0)); ?>
									<?php
									echo $form->labelEx($invoice, 'automatic_numbering', array(
										'class' => 'tooltip-top',
										'title' => 'Automatically assign the next available number if the invoice number is taken',
										'label' => 'Automatic'
									));
									?>
								<?php endif; ?>
								<?php echo $form->error($invoice, 'invoice_number', array(), true, true); ?>
								<?php echo $form->hiddenField($invoice, 'id'); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120"><?php echo $form->labelEx($invoice, 'invoice_date'); ?></th>
							<td>
								<?php
								$form->widget('PsJuiDatePicker', array(
									'model' => $invoice,
									'attribute' => 'invoice_date',
									'options' => array(
										'showButtonPanel' => false,
										'changeYear' => true,
										'dateFormat' => 'dd-mm-yy',
									),
								));
								?>
								<?php echo $form->error($invoice, 'invoice_date', false); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120"><?php echo $form->labelEx($invoice, 'due_date', array('label' => 'Due Date')); ?></th>
							<td>
								<?php
								$form->widget('PsJuiDatePicker', array(
									'model' => $invoice,
									'attribute' => 'due_date',
									'options' => array(
										'showButtonPanel' => false,
										'changeYear' => true,
										'dateFormat' => 'dd-mm-yy',
									),
								));
								?>
								<?php echo $form->error($invoice, 'due_date', false); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120"><?php echo $form->labelEx($invoice, 'terms', array('label' => 'Terms of Payment')); ?></th>
							<td>
								<?php echo CHtml::dropDownList('Invoice_terms_select', (in_array($invoice->terms, Invoice::getStandardInvoiceTerms()) ? $invoice->terms : 'Other'), Invoice::getStandardInvoiceTerms(), array('prompt' => '')); ?>
								<?php echo $form->textField($invoice, 'terms', array('style' => (in_array($invoice->terms, Invoice::getStandardInvoiceTerms()) ? 'display: none;' : ''), 'placeholder' => 'Enter term...')); ?>
								<?php echo $form->error($invoice, 'terms', false); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120"><?php echo $form->labelEx($invoice, 'description', array('label' => 'Miscellaneous Info')); ?></th>
							<td>
								<?php
								echo $form->textField($invoice, 'description', array('size' => 43));
								?>
								<?php echo $form->error($invoice, 'description', false); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120"><?php echo $form->labelEx($invoice, 'currency_code'); ?></th>
							<td>
								<?php
								echo $form->dropDownList($invoice, 'currency_code', array('SGD' => 'SGD - Singapore Dollar'));
								?>
								<?php echo $form->error($invoice, 'currency_code', false); ?>
							</td>
						</tr>
						<tr>
							<th scope="row" width="120"></th>
							<td>
								<?php echo $form->checkbox($invoice, 'visible_to_sp'); ?>
								<?php echo $form->labelEx($invoice, 'visible_to_sp', array('label' => 'Show Salesperson\'s Copy')); ?>
								<?php echo $form->error($invoice, 'visible_to_sp', false); ?>
							</td>
						</tr>
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="grid_16">
    <div class="section">
		<div class="block">
			<h2>Invoice Items</h2>		
			<div id="invoice-items-ct" class="clearfix">
				<div id="invoice-items">
					<table class="form-table">
						<thead>    
							<tr>
								<th width="*">Item / Description</th>
								<th class="qty hidden" width="80">Qty</th>
								<th class="unit-price" width="80">Unit Price</th>
								<th class="account-id" width="100">Account</th>
								<th class="gst-amount" width="100">GST</th>
								<th class="item-amount" width="150" class="align-right">Amount <span class="currency"><?php echo $invoice->currency_code; ?></span></th>
								<th class="remove-invoice-item-column" width="16"></th>
							</tr>
						</thead>
						<?php if (!empty($invoiceItems)): ?>
							<?php foreach ($invoiceItems as $i => $invoiceItem): ?>
								<tbody>
									<tr>
										<td>
											<?php echo $form->textArea($invoiceItem, '[' . $i . ']description', array('rows' => 4, 'placeholder' => '')); ?>
											<?php echo $form->error($invoiceItem, '[' . $i . ']description', false); ?>
											<?php echo $form->hiddenField($invoiceItem, '[' . $i . ']id'); ?>
											<span class="float-right">Transactions &nbsp; <img rowId="<?php echo $i; ?>" src="/images/icons/farmfresh/16x16/house_go.png" class="btnChooseTransaction" title="Choose from Transactions"/></span>
											<?php echo $form->hiddenField($invoiceItem, '[' . $i . ']property_case_id'); ?>
										</td>
										<td class="qty hidden">
											<div class="form-block">
												<?php
												$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
													'model' => $invoiceItem,
													'attribute' => '[' . $i . ']quantity',
													'htmlOptions' => array(
														'class' => 'align-right',
														'size' => '10'
													),
													'options' => array(
														'vMax' => '9999999.99'
													)
												));
												?>
												<?php echo $form->error($invoiceItem, '[' . $i . ']quantity', false); ?>
											</div>
										</td>
										<td class="unit-price">
											<div class="form-block">
												<?php
												$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
													'model' => $invoiceItem,
													'attribute' => '[' . $i . ']unit_price',
													'htmlOptions' => array(
														'class' => 'align-right',
														'size' => '10'
													)
												));
												?>
												<?php echo $form->error($invoiceItem, '[' . $i . ']unit_price', false); ?>
											</div>
										</td>
										<td class="account-id">
											<div class="account-select">
												<?php
												echo CHtml::dropDownList('account_id[]', '', array(
													'Commission Income'
												));
												?>
											</div>
										</td>
										<td class="gst-amount">
											<div class="form-block">
												<?php echo $form->checkBox($invoiceItem, '[' . $i . ']is_taxable'); ?>
												<?php
												$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
													'model' => $invoiceItem,
//													'attribute' => '[' . (isset($i) ? $i + 1 : 0) . ']gst_amount',
													'attribute' => '[' . $i . ']gst_amount',
													'htmlOptions' => array(
														'class' => 'align-right',
														'size' => '5',
													)
												));
												?>
												<?php // echo $form->error($newInvoiceItem, '[' . (isset($i) ? $i + 1 : 0) . ']gst_amount', false); ?>
												<?php echo $form->error($newInvoiceItem, '[' . $i . ']gst_amount', false); ?>
											</div>
										</td>
										<td class="item-amount">
											<div class="form-block">
												<?php
												$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
													'model' => $invoiceItem,
													'attribute' => '[' . $i . ']amount',
													'htmlOptions' => array(
														'class' => 'align-right',
														'size' => '20',
//														'disabled' => true
													),
													'options' => array(
														'aSign' => $invoice->currency_code . ' '
													)
												));
												?>
												<?php echo $form->error($invoiceItem, '[' . $i . ']amount', false); ?>
											</div>
										</td>
										<td>
											<div class="align-right">								
												<a href="javascript:void(0);" class="blue link remove-invoice-item" tabindex="-1" style="<?php echo (empty($invoiceItems) ? "display: none" : ""); ?>"><img class="valign-middle"src="/images/icons/fugue/cross-button.png" width="16" height="16" /></a>
											</div>
										</td>
									</tr>
								</tbody>
							<?php endforeach; ?>
						<?php else: ?>
							<tbody class="<?php echo (empty($invoiceItems) ? "" : "inactive"); ?>">
								<tr>
									<td>
										<?php echo $form->textArea($newInvoiceItem, '[' . (isset($i) ? $i + 1 : 0) . ']description', array('rows' => 4, 'placeholder' => '')); ?>
										<?php echo $form->error($newInvoiceItem, '[' . (isset($i) ? $i + 1 : 0) . ']description', false); ?>
										<span class="float-right">Transactions &nbsp; <img rowId="<?php echo (isset($i) ? $i + 1 : 0); ?>" src="/images/icons/farmfresh/16x16/house_go.png" class="btnChooseTransaction" title="Choose from Transactions"/></span>
										<?php echo $form->hiddenField($newInvoiceItem, '[' . (isset($i) ? $i + 1 : 0) . ']property_case_id'); ?>
									</td>
									<td class="qty hidden">
										<div class="form-block">
											<?php
											$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
												'model' => $newInvoiceItem,
												'attribute' => '[' . (isset($i) ? $i + 1 : 0) . ']quantity',
												'htmlOptions' => array(
													'class' => 'align-right',
													'size' => '10'
												),
												'options' => array(
													'vMax' => '9999999.99'
												)
											));
											?>
											<?php echo $form->error($newInvoiceItem, 'quantity', false); ?>
										</div>
									</td>
									<td class="unit-price">
										<div class="form-block">
											<?php
											$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
												'model' => $newInvoiceItem,
												'attribute' => '[' . (isset($i) ? $i + 1 : 0) . ']unit_price',
												'htmlOptions' => array(
													'class' => 'align-right',
													'size' => '10'
												)
											));
											?>
											<?php echo $form->error($newInvoiceItem, 'unit_price', false); ?>
										</div>
									</td>
									<td class="account-id">
										<div class="account-select">
											<?php
											echo CHtml::dropDownList('account_id[]', '', array(
												'Commission Income'
											));
											?>
										</div>
									</td>
									<td class="gst-amount">
										<div class="form-block">
											<?php echo $form->checkBox($newInvoiceItem, '[' . (isset($i) ? $i + 1 : 0) . ']is_taxable'); ?>
											<?php
											$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
												'model' => $newInvoiceItem,
												'attribute' => '[' . (isset($i) ? $i + 1 : 0) . ']gst_amount',
												'htmlOptions' => array(
													'class' => 'align-right',
													'size' => '5',
												)
											));
											?>
											<?php echo $form->error($newInvoiceItem, '[' . (isset($i) ? $i + 1 : 0) . ']gst_amount', false); ?>
										</div>
									</td>
									<td class="item-amount">
										<div class="form-block">
											<?php
											$this->widget('common.widgets.PsAutoNumeric.PsAutoNumeric', array(
												'model' => $newInvoiceItem,
												'attribute' => '[' . (isset($i) ? $i + 1 : 0) . ']amount',
												'htmlOptions' => array(
													'class' => 'align-right',
													'size' => '20',
//													'disabled' => true
												),
												'options' => array(
													'aSign' => $invoice->currency_code . ' '
												)
											));
											?>
											<?php echo $form->error($newInvoiceItem, '[' . (isset($i) ? $i + 1 : 0) . ']amount', false); ?>
										</div>
									</td>
									<td class="remove-invoice-item-column">
										<a href="javascript:void(0);" class="blue link remove-invoice-item" tabindex="-1" style="<?php echo (empty($invoiceItems) ? "display: none" : ""); ?>"><img class="valign-middle"src="/images/icons/fugue/cross-button.png" width="16" height="16" /></a>
									</td>
								</tr>
							</tbody>
						<?php endif; ?>
					</table>
				</div>
				<a id="btnAddInvoiceItem" href="javascript:void(0);" class="blue link">
					<img class="valign-middle"src="/images/icons/fugue/plus-button.png" width="16" height="16" /> Add a new line
				</a>
				<div class="subtotal-table">
					<table class="form-table">
						<tbody>
							<tr id="subtotal">
								<th scope="row">Subtotal</th>
								<td><?php echo $invoice->currency_code; ?> <?php echo $invoice->getInvoiceSubtotal(); ?></td>
							</tr>
							<tr id="grandTotal">
								<th scope="row">Total (<?php echo $invoice->currency_code; ?>)</th>
								<td><?php echo $invoice->currency_code; ?> <?php echo $invoice->getInvoiceTotal(); ?></td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>
		</div>
    </div>
</div>

<div class="grid_16">
    <button type="submit" class="link-button with-icon green no-margin" id="btnFinish">
        <span>Save Invoice</span>
    </button>
	&nbsp;or <a class="link" href="<?php echo $this->createUrl('/invoice/index'); ?>">Cancel</a>
</div>
<?php $this->endWidget(); ?>